import java.util.PriorityQueue;

/**
* @description: 1046. 最后一块石头的重量
* @author hewei
* @date 2022/10/6 16:31
* @version 1.0
*/

public class LastStoneWeight {

    public static void main(String[] args) {
        int[] nums = {31,26,33,21,40};
        System.out.println(new LastStoneWeight().lastStoneWeight(nums));
    }

    public int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a);
        for (int stone : stones) {
            heap.add(stone);
        }
        while (heap.size() >= 2) {
            Integer poll1 = heap.poll();
            Integer poll2 = heap.poll();
            if (poll1 - poll2 != 0) heap.add(poll1 - poll2);
        }
        return heap.size() == 1 ? heap.poll() : 0;
    }
}
